﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "计划工单管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var workOrderStatuses = ViewBag.WorkOrderStatusEnums as List<KeyValuePair<int, string>>;
}
@section Contents{
    <style>

        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkShop}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有车间</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.OrderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" value="@ViewBag.OrderNo" @(!string.IsNullOrWhiteSpace(ViewBag.OrderNo) ? " disabled " : "") autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkorderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtWorkOrderNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.ProduceStatus}}：</label>
                        <div class="layui-input-inline">
                            <select id="selStatus" lay-filter="selStatus">
                                <option value="">所有状态</option>
                                @foreach (var item in workOrderStatuses)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.WorkorderDate}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" value="@(DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" id="btnCreateWorkOrder">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/WorkOrder/QueryWorkOrders'
                    , method: "post"
                    , where: { workshopCode: '', orderNo: "", workOrderNo: "", status: null, startDate: "@DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd")", endDate: "@DateTime.Today.ToString("yyyy-MM-dd")" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type:"numbers", width: 60 }
                        , { field: 'WorkOrderNo', title: '工单号', minWidth: 200 }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150 }
                        , { field: 'ProductCode', title: '成品料号', minWidth: 150 }
                        , { field: 'ProductName', title: '成品名称', minWidth: 250 }
                        , { field: 'Count', title: '数量', minWidth: 70 }
                        , {
                            title: '生产状态', minWidth: 100, templet: function (d) {
                                var color = "#666";
                                if (d.Status == 1) {
                                    color = "#FFB800";
                                } else if (d.Status == 2) {
                                    color = "#FB800";
                                }
                                return "<span style='color:" + color + ";'>" + d.CNStatus + "</span>";
                            }
                        }
                        , { field: 'CustomerCode', title: '客户代码', minWidth: 150 }
                        , { field: 'CustomerName', title: '客户名称', minWidth: 150 }
                        , { field: 'WorkshopName', title: '生产车间', minWidth: 150 }
                        , { field: 'StartDateStr', title: '开工日期', minWidth: 150 }
                        , { field: 'EndDateStr', title: '完工日期', minWidth: 150 }
                        , { field: 'WorkOrderTimeStr', title: '工单时间', minWidth: 150 }
                        , {
                            title: '操作', width: 250, fixed: "right", align:"center", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="baseEdit" style="margin-bottom:5px;"><i class="layui-icon">&#xe642;</i>基础信息</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="productionEdit" style="margin-bottom:5px;"><i class="layui-icon">&#xe642;</i>生产编辑</button><br/>\
                                           <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="componentEdit" style="margin-bottom:5px;"><i class="layui-icon">&#xe614;</i>条码管理</button>\
                                           <button class="layui-btn layui-btn-sm" lay-event="allocateOrder" style="margin-bottom:5px;"><i class="layui-icon">&#xe674;</i>调拨单</button>';
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.orderNo = $("#txtOrderNo").val();
                    options.where.workshopCode = $("#selWorkshops").val();
                    options.where.workOrderNo = $("#txtWorkOrderNo").val();
                    options.where.status = $("#selStatus").val() == "" ? null : parseInt($("#selStatus").val());
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    options.where.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endDate = rangeTime.length <= 0 ? null : rangeTime[1];

                    if (table.index <= 0) {
                        table.render(options);
                        table.on("tool(table)", function (obj) {
                            switch (obj.event) {
                                case "baseEdit":
                                    parent.layui.index.openTabsPage("/Plan/CreateWorkOrder?workOrderNo=" + obj.data.WorkOrderNo, "工单编辑(" + obj.data.WorkOrderNo + ")");
                                    break;
                                case "productionEdit":
                                    parent.layui.index.openTabsPage("/Plan/EditWorkOrder?workOrderNo=" + obj.data.WorkOrderNo, "生产编辑(" + obj.data.WorkOrderNo + ")");
                                    break;
                                case "componentEdit":
                                    parent.layui.index.openTabsPage("/Plan/ComponentEdit?workOrderNo=" + obj.data.WorkOrderNo, "条码管理(" + obj.data.WorkOrderNo + ")");
                                    break;
                                case "allocateOrder":
                                    parent.layui.index.openTabsPage("/Plan/AllocateOrderMgr?workOrderNo=" + obj.data.WorkOrderNo, "调拨单管理");
                                    break;
                            }
                        });
                    } else {
                        table.reload("table", options);
                    }
                }

                $("#btnCreateWorkOrder").click(function () {
                    layer.confirm('该工单仅在ERP网络异常无法同步时使用，确定要“手动创建工单”吗？？？', { icon: 3, title: '提示' }, function (index) {
                        parent.layui.index.openTabsPage("/Plan/CreateWorkOrder", "手动创建工单");
                    });
                });
            });
        }
    </script>
}
